package com.project.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.framework.core.result.APIObjResponse;
import com.framework.core.result.BasePage;
import com.project.base.BaseController;
import com.project.biz.CmsRecoveryMemberBiz;
import com.project.biz.CmsRecoveryMemberRechargeBiz;
import com.project.biz.CmsSystemConfBiz;
import com.project.common.model.UserInfo;
import com.project.common.utils.IpageToPage;
import com.project.entity.CmsRecoveryMember;
import com.project.entity.CmsSystemConf;
import com.project.pojo.dto.RecoveryMemberDTO;
import com.project.pojo.dto.RecoveryMemberRechargeDTO;
import com.project.pojo.model.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;

/**
 * @Description: 回收员
 * @Author: zhuchi
 * @CreateDate: 2020/11/9 13:14
 * @Version: 1.0
 */
@Api(value = "CmsRecoveryMemberController", description = "回收员")
@RestController
@RequestMapping("/recovery")
@Slf4j
public class CmsRecoveryMemberController extends BaseController {

    /**
     * 回收员业务层接口类
     */
    @Autowired
    private CmsRecoveryMemberBiz cmsRecoveryMemberBiz;

    /**
     * 回收员充值业务层接口类
     */
    @Autowired
    private CmsRecoveryMemberRechargeBiz recoveryMemberRechargeBiz;

    /**
     * 系统配置业务层接口类
     */
    @Autowired
    private CmsSystemConfBiz systemConfBiz;

    /**
     * 后台-查询回收员列表
     * @param getAllRecoveryMemberModel
     * @return
     */
    @ApiOperation(value = "后台-查询回收员列表", httpMethod = "POST")
    @PostMapping(value = "/getAllRecoveryMember")
    public APIObjResponse<BasePage<RecoveryMemberDTO>> getAllRecoveryMember(@RequestBody GetAllRecoveryMemberModel getAllRecoveryMemberModel){
        IPage<RecoveryMemberDTO> iPage = this.cmsRecoveryMemberBiz.getAllRecoveryMember(getAllRecoveryMemberModel);
        BasePage<RecoveryMemberDTO> basePage = IpageToPage.toBasePage(iPage, RecoveryMemberDTO.class);

        return APIObjResponse.success("查询成功", basePage);
    }

    /**
     * 申请回收员
     * @param applyRecoveryMemberModel
     * @return
     */
    @ApiOperation(value = "申请回收员", httpMethod = "POST")
    @PostMapping(value = "/applyRecoveryMember")
    public APIObjResponse<Integer> applyRecoveryMember(@Validated @RequestBody ApplyRecoveryMemberModel applyRecoveryMemberModel){
        UserInfo userInfo = super.getLoginUser();
        int result = this.cmsRecoveryMemberBiz.addApplyRecoveryMember(applyRecoveryMemberModel, userInfo.getBaseUserId());

        return APIObjResponse.success("操作成功", result);
    }

    /**
     * 后台-同意回收员申请
     * @param recoveryMemberId 回收员ID
     * @return
     */
    @ApiOperation(value = "后台-同意回收员申请,参数{recoveryMemberId : 回收员ID}", httpMethod = "GET")
    @GetMapping(value = "/agreeApplyRecoveryMember")
    public APIObjResponse<Integer> agreeApplyRecoveryMember(@RequestParam("recoveryMemberId") Long recoveryMemberId){
        UserInfo userInfo = super.getLoginUser();
        int result = this.cmsRecoveryMemberBiz.updateAgreeApplyRecoveryMember(recoveryMemberId, userInfo.getBaseUserId());

        return APIObjResponse.success("操作成功", result);
    }

    /**
     * 后台-拒绝回收员申请
     * @param refuseApplyRecoveryMemberModel
     * @return
     */
    @ApiOperation(value = "后台-拒绝回收员申请", httpMethod = "POST")
    @PostMapping(value = "/refuseApplyRecoveryMember")
    public APIObjResponse<Integer> refuseApplyRecoveryMember(@Validated @RequestBody RefuseApplyRecoveryMemberModel refuseApplyRecoveryMemberModel){
        UserInfo userInfo = super.getLoginUser();
        int result = this.cmsRecoveryMemberBiz.updateRefuseApplyRecoveryMember(refuseApplyRecoveryMemberModel, userInfo.getBaseUserId());

        return APIObjResponse.success("操作成功", result);
    }

    /**
     * 获取当前登录用户回收员信息
     * @return
     */
    @ApiOperation(value = "获取当前登录用户回收员信息", httpMethod = "GET")
    @GetMapping(value = "/getMyRecoveryMember")
    public APIObjResponse<CmsRecoveryMember> getMyRecoveryMember(){
        UserInfo userInfo = super.getLoginUser();
        CmsRecoveryMember recoveryMember = this.cmsRecoveryMemberBiz.getRecoveryMmberByUserId(userInfo.getBaseUserId());
        if (null == recoveryMember){
            recoveryMember = new CmsRecoveryMember();
            recoveryMember.setState(0);//状态（0:未申请,1:待审核,2:通过,3:驳回）
        }

        return APIObjResponse.success("查询成功", recoveryMember);
    }

    /**
     * 根据回收员ID获取回收员信息
     * @return
     */
    @ApiOperation(value = "根据回收员ID获取回收员信息", httpMethod = "GET")
    @GetMapping(value = "/getRecoveryMemberDetail")
    public APIObjResponse<CmsRecoveryMember> getRecoveryMemberDetail(@RequestParam("recoveryMemberId") Long recoveryMemberId){
        CmsRecoveryMember recoveryMember = this.cmsRecoveryMemberBiz.getById(recoveryMemberId);
        if (null == recoveryMember){
            return APIObjResponse.error("未查到回收员信息");
        }

        return APIObjResponse.success("查询成功", recoveryMember);
    }

    /**
     * 回收员充值
     * @param recoveryMemberRechargeModel
     * @return
     */
    @ApiOperation(value = "回收员充值", httpMethod = "POST")
    @PostMapping(value = "/addRecoveryMemberRecharge")
    public APIObjResponse<RecoveryMemberRechargeDTO> addRecoveryMemberRecharge(@Validated @RequestBody RecoveryMemberRechargeModel recoveryMemberRechargeModel){
        UserInfo userInfo = super.getLoginUser();
        CmsSystemConf systemConf = this.systemConfBiz.getSystemConf();
        BigDecimal minRecharge = systemConf.getMinRecharge();
        if (recoveryMemberRechargeModel.getTotalAmount().compareTo(minRecharge) == -1){
            return APIObjResponse.error("充值金额最少为"+minRecharge+"元");
        }

        recoveryMemberRechargeModel.setIp(super.getRealIP());
        RecoveryMemberRechargeDTO result = this.recoveryMemberRechargeBiz.addRecoveryMemberRecharge(recoveryMemberRechargeModel, userInfo.getBaseUserId());
        return APIObjResponse.success("操作成功", result);
    }

    /**
     * 后台-停用/启用回收员
     * @param stopRecoveryMemberModel
     * @return
     */
    @ApiOperation(value = "后台-停用/启用回收员", httpMethod = "POST")
    @PostMapping(value = "/stopRecoveryMember")
    public APIObjResponse<Integer> stopRecoveryMember(@Validated @RequestBody StopRecoveryMemberModel stopRecoveryMemberModel){
        UserInfo userInfo = super.getLoginUser();
        int result = this.cmsRecoveryMemberBiz.updateStopRecoveryMember(stopRecoveryMemberModel, userInfo.getBaseUserId());

        return APIObjResponse.success("操作成功", result);
    }

}
